package xyz.xtt.schedule.support;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import xyz.xtt.schedule.entity.TblLuckPhonePool;
import xyz.xtt.schedule.service.LuckPhoneService;

/**
 * Spring容器启动完毕事件监听器
 *
 * @author dale
 * @since 2025/1/7
 **/
@Component
@Slf4j
public class StartupListener implements ApplicationListener<ApplicationReadyEvent> {
    @Autowired
    private LuckPhoneService luckPhoneService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 在容器启动完成后执行逻辑
        log.info("Spring 容器已启动完成...");

        LambdaQueryChainWrapper<TblLuckPhonePool> queryWrapper = new LambdaQueryChainWrapper<>(luckPhoneService.getBaseMapper());
        Long phoneCount = queryWrapper.count();
        log.info("手机号码池数量：{}", phoneCount);
        if (phoneCount == 0) {
            new Thread(()->{luckPhoneService.synPhoneData();}).start();
        }
    }
}
